﻿import com.dmotiko.util.*
import com.dmotiko.general.*
import com.dmotiko.mirador.*
import mx.transitions.easing.Regular;
import mx.transitions.Tween;
import mx.utils.Delegate;

class MiradorClients
extends BaseClip {	
	private var mcl:MovieClipLoader;
	private var mcContainer:MovieClip;
	private var nClient:Number;
	private var timer:MovieClip;
		
	private function initClip():Void {
		mcl = new MovieClipLoader();
		mcl.addListener(this);
		mcContainer = this.createEmptyMovieClip("mcContainer", this.getNextHighestDepth());
		timer = this.createEmptyMovieClip("timer", this.getNextHighestDepth());
		timer.startTimer = function(nDelay, fListener) {
			var nTime:Number = getTimer();
			this.onEnterFrame = function() {
				if ( getTimer() - nTime > nDelay ) {
					nTime = getTimer();
					fListener();
				}
			}
		}
		timer.stopTimer = function() {
			delete this.onEnterFrame;
		}
		
		this.setData( MiradorSite.getApp().getClients() );
	}	
		
	private function refreshData():Void {
		mcContainer = this.createEmptyMovieClip("mcContainer", mcContainer.getDepth());
		mcContainer.createEmptyMovieClip("img", 1);
		nClient = 0;
		mcl.loadClip( data[0][nClient].src, mcContainer.img);
	}
	
	public function onLoadInit( target:MovieClip ):Void {
		target._y = (this._height - target._height) / 2 - 15;
		target._x = (this._width - target._width) / 2;
		target._alpha = 0;
		registerTween("clientFadeIn", new Tween( target, "_alpha", Regular.easeOut, target._alpha, 100, 0.7, true), false, true);
		registerTween("clientY", new Tween( target, "_y", Regular.easeOut, target._y-30, target._y, 0.7, true));
	}
	
	private function tweenFinished( key:String, tween:Tween):Void {
		if (key == "clientFadeIn") {
			timer.startTimer( 2000, Delegate.create(this, timer_complete) );
		} else {
			nClient++;
			if (nClient == data[0].length) nClient = 0;
			mcl.loadClip( data[0][nClient].src, mcContainer);
		}
	}
	
	private function timer_complete():Void {
		timer.stopTimer();
		registerTween("clientFadeOut", new Tween( mcContainer, "_alpha", Regular.easeOut, mcContainer._alpha, 0, 0.7, true), false, true);
		registerTween("clientY", new Tween( mcContainer, "_y", Regular.easeOut, mcContainer._y, mcContainer._y+50, 0.7, true));
	}
	
}